package com.sc.web.gwt.client.view.administratorwidget.contents;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiConstructor;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import com.sc.web.gwt.client.service.AgentAdminDataManagementService;
import com.sc.web.gwt.client.service.AgentAdminDataManagementServiceAsync;
import com.sc.web.gwt.client.view.administratorwidget.AdministratorWidgetResources;
import com.sc.web.gwt.shared.model.AgentAdmin;

import java.util.ArrayList;
import java.util.List;

public class ViewAllAgentsWidget extends Composite {
    private static Binder binder = GWT.create(Binder.class);

    public interface Binder extends UiBinder<HTMLPanel, ViewAllAgentsWidget> {
    }

    AgentAdminDataManagementServiceAsync agentAdminDataManagementServiceAsync = GWT.create(AgentAdminDataManagementService.class);
    private List<AgentAdmin> allAgentAdmins = new ArrayList<AgentAdmin>();
    final AdministratorWidgetResources resources = GWT.create(AdministratorWidgetResources.class);
    final ViewAllAgentsWidget thisWidget = this;
    private Grid<AgentAdmin> grid;
    private ContentPanel gridPanel;
    boolean isViewAll = false;

    @UiField
    HTMLPanel topPanel;
    @UiField
    Button searchBtn;
    @UiField
    ListBox searchCB;
    @UiField
    TextBox searchText;
    @UiField
    HTMLPanel searchPanel;


    @UiConstructor
    public ViewAllAgentsWidget(boolean isViewAll) {
        initWidget(binder.createAndBindUi(this));
        this.isViewAll = isViewAll;
        if(isViewAll){
            searchPanel.getElement().getStyle().setDisplay(Style.Display.NONE);
            agentAdminDataManagementServiceAsync.getAllAgentAdmins(new AsyncCallback<List<AgentAdmin>>() {
                public void onFailure(Throwable throwable) {
                    throwable.printStackTrace();
                }

                public void onSuccess(List<AgentAdmin> agentAdmins) {
                    allAgentAdmins.addAll(agentAdmins);
                    initDataTable();
                }
            });
        }
        else{
            searchCB.addItem("ID");
            searchCB.addItem("Account");
            searchCB.addItem("Full Name");
            searchCB.addItem("Email");
            searchCB.addItem("Phone No.");
            searchCB.addItem("Position");
            searchCB.addItem("Company");
            searchCB.addItem("Site");
            searchCB.addItem("State");

            searchBtn.addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {
                    agentAdminDataManagementServiceAsync.searchAgentAdmins(searchText.getText(), searchCB.getSelectedIndex(), new AsyncCallback<List<AgentAdmin>>() {
                        public void onFailure(Throwable throwable) {
                            throwable.printStackTrace();
                        }

                        public void onSuccess(List<AgentAdmin> result) {
                            allAgentAdmins = new ArrayList<AgentAdmin>();
                            allAgentAdmins.addAll(result);
                            grid.getStore().removeAll();
                            grid.getStore().add(allAgentAdmins);
                            grid.getStore().commitChanges();
                        }
                    });
                }
            });
            initDataTable();
        }
    }

    private void initDataTable() {
        List<ColumnConfig> configs = new ArrayList<ColumnConfig>();
        ColumnConfig column = new ColumnConfig();
        column.setId("agentAdminId");
        column.setHeader("ID");
        column.setWidth(50);
        configs.add(column);

        column = new ColumnConfig();
        column.setId("agentAdminAccount");
        column.setHeader("Account");
        column.setWidth(70);
        configs.add(column);

        column = new ColumnConfig();
        column.setId("agentAdminName");
        column.setHeader("Full Name");
        column.setWidth(150);
        configs.add(column);

        column = new ColumnConfig();
        column.setId("agentAdminEmail");
        column.setHeader("Email");
        column.setWidth(70);
        configs.add(column);

        column = new ColumnConfig();
        column.setId("agentAdminPhoneNumber");
        column.setHeader("Phone No.");
        column.setWidth(70);
        configs.add(column);

        column = new ColumnConfig();
        column.setId("agentAdminPosition");
        column.setHeader("Position");
        column.setWidth(70);
        configs.add(column);

        column = new ColumnConfig();
        column.setId("agentAdminCompany");
        column.setHeader("Company");
        column.setWidth(70);
        configs.add(column);

        column = new ColumnConfig();
        column.setId("siteName");
        column.setHeader("Site");
        column.setWidth(70);
        configs.add(column);

        column = new ColumnConfig();
        column.setId("state");
        column.setHeader("State");
        column.setWidth(70);
        configs.add(column);


        final ListStore<AgentAdmin> store = new ListStore<AgentAdmin>();
        store.add(allAgentAdmins);
        ColumnModel cm = new ColumnModel(configs);

        gridPanel = new ContentPanel();
        gridPanel.setHeading("View All Agents");
        gridPanel.setFrame(true);
        gridPanel.setIcon(resources.ICONS.table());

        grid = new Grid<AgentAdmin>(store, cm);
        grid.setAutoExpandColumn("agentAdminName");
        grid.setBorders(true);
        grid.setHeight("100%");
        grid.setTrackMouseOver(true);
        grid.setStripeRows(true);
        gridPanel.add(grid);

        gridPanel.setSize("100%", "500px");
        gridPanel.setLayout(new FitLayout());

        ToolBar toolBar = new ToolBar();
        com.extjs.gxt.ui.client.widget.button.Button addBtn = new com.extjs.gxt.ui.client.widget.button.Button("Add new agent");
        addBtn.addSelectionListener(new SelectionListener<ButtonEvent>() {
            @Override
            public void componentSelected(ButtonEvent ce) {
                new AddOrEditAgentAdminWidget(new AgentAdmin(), thisWidget).showWidget();
            }
        });
        toolBar.add(addBtn);

        com.extjs.gxt.ui.client.widget.button.Button editBtn = new com.extjs.gxt.ui.client.widget.button.Button("Edit selected agent");
        editBtn.addSelectionListener(new SelectionListener<ButtonEvent>() {
            @Override
            public void componentSelected(ButtonEvent ce) {
                AgentAdmin agentAdmin = grid.getSelectionModel().getSelectedItem();
                if(agentAdmin!=null){
                    new AddOrEditAgentAdminWidget(agentAdmin, thisWidget).showWidget();
                }
            }
        });
        toolBar.add(editBtn);
        gridPanel.setTopComponent(toolBar);

        topPanel.add(gridPanel);
    }

    public void refreshTable(){
        agentAdminDataManagementServiceAsync.getAllAgentAdmins(
                new AsyncCallback<List<AgentAdmin>>() {
                    public void onFailure(Throwable throwable) {
                        throwable.printStackTrace();
                    }

                    public void onSuccess(List<AgentAdmin> agentAdmin) {
                        allAgentAdmins = new ArrayList<AgentAdmin>();
                        allAgentAdmins.addAll(agentAdmin);
                        grid.getStore().removeAll();
                        grid.getStore().add(allAgentAdmins);
                        grid.getStore().commitChanges();
                    }
                }
        );
    }
}
